package com.jza04.flightbooking.service.impl;

import com.jza04.flightbooking.entity.Passenger;
import com.jza04.flightbooking.mapper.PassengerMapper;
import com.jza04.flightbooking.service.PassengerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 旅客服务实现类
 */
@Service
public class PassengerServiceImpl implements PassengerService {

    @Autowired
    private PassengerMapper passengerMapper;
    
    @Override
    public List<Passenger> findAllPassengers() {
        return passengerMapper.findAll();
    }
    
    @Override
    public Passenger findPassengerById(Integer passengerId) {
        return passengerMapper.findById(passengerId);
    }
    
    @Override
    public Passenger findPassengerByIdCard(String idCard) {
        return passengerMapper.findByIdCard(idCard);
    }
    
    @Override
    @Transactional
    public boolean addPassenger(Passenger passenger) {
        // 设置默认值
        if (passenger.getTotalPurchase() == null) {
            passenger.setTotalPurchase(new BigDecimal("0.00"));
        }
        if (passenger.getVipDiscount() == null) {
            passenger.setVipDiscount(new BigDecimal("1.00"));
        }
        
        return passengerMapper.insert(passenger) > 0;
    }
    
    @Override
    @Transactional
    public boolean updatePassenger(Passenger passenger) {
        return passengerMapper.update(passenger) > 0;
    }
    
    @Override
    @Transactional
    public boolean deletePassenger(Integer passengerId) {
        return passengerMapper.delete(passengerId) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateTotalPurchase(Integer passengerId, BigDecimal amount) {
        boolean result = passengerMapper.updateTotalPurchase(passengerId, amount) > 0;
        
        // 更新完总购票金额后，根据新的总额更新VIP折扣
        if (result) {
            Passenger passenger = findPassengerById(passengerId);
            if (passenger != null) {
                BigDecimal totalPurchase = passenger.getTotalPurchase();
                BigDecimal vipDiscount;
                
                if (totalPurchase.compareTo(new BigDecimal("10000")) >= 0) {
                    vipDiscount = new BigDecimal("0.80");
                } else if (totalPurchase.compareTo(new BigDecimal("5000")) >= 0) {
                    vipDiscount = new BigDecimal("0.85");
                } else if (totalPurchase.compareTo(new BigDecimal("3000")) >= 0) {
                    vipDiscount = new BigDecimal("0.90");
                } else if (totalPurchase.compareTo(new BigDecimal("1000")) >= 0) {
                    vipDiscount = new BigDecimal("0.95");
                } else {
                    vipDiscount = new BigDecimal("1.00");
                }
                
                updateVipDiscount(passengerId, vipDiscount);
            }
        }
        
        return result;
    }
    
    @Override
    @Transactional
    public boolean updateVipDiscount(Integer passengerId, BigDecimal vipDiscount) {
        return passengerMapper.updateVipDiscount(passengerId, vipDiscount) > 0;
    }
} 